'use strict';

import {ajax, nameSpace } from 'utils/index';
import jsonp from 'jsonp';

let ns = nameSpace('RingZhekouEditItems');

export const GET_LIST_DONE = ns('GET_LIST_DONE');
export const CLEAR_LIST = ns('CLEAR_LIST');
export const CLOSE_DIALOG = ns('CLOSE_DIALOG');
export const RESET_ITEM = ns('RESET_ITEM');
export const REMOVE_ITEM = ns('REMOVE_ITEM');
export const LOAD_START = ns('LOAD_START');
export const LOAD_ERR = ns('LOAD_ERR');

export function clearList() {
  return {type: CLEAR_LIST};
}
export function closeDialog() {
  return {type: CLOSE_DIALOG,visible:false};
}
export function removeItem(activity_id,detail_id,act_type) {
	return (dispatch) => {
		//发送请求前
		dispatch({
		  type: LOAD_START
		});
		if(window.workbench){
			//千牛环境执行
			let cmd = "";
			switch(act_type){
			  case "limit":
				cmd  = "taobao.ump.detail.delete";
			  break;
			  case "item":
				cmd = "taobao.promotionmisc.common.item.detail.delete";
			  break;
			}
			  window.QN.top.invoke( {
					cmd : cmd,
					param : {activity_id,detail_id},
					method:"post",
					success : function(rsp, cmd, param) {
						console.log(rsp,param);
					},
					error : function(msg, cmd, param) {
						console.log(msg,param);
					}
				} );
		}else{
			//web环境执行创建折扣
			let cmd = "";
			switch(act_type){
			  case "limit":
				cmd = "removeLimitInActive";
			  break;
			  case "item":
				cmd = "removeItemsInActive";
			  break;
			}
			ajax({
				  api: cmd,
				  method: 'GET',
				  data:{activity_id,detail_id}
				},(json) => {
					ajax({
					  api: 'removeItemsInRingDatabase',
					  method: 'GET',
					  data:{activity_id,detail_id}
					},(json) => {
						dispatch({
						  type: REMOVE_ITEM,
						  activity_id,
						  detail_id
						});
					},(json) => {
					  //失败后
					  //(json.code==53) W2超时
					  const login = (json.code==210);
					  dispatch({
						type: LOAD_ERR,
						message:json.msg+(json.sub_msg?json.sub_msg:""),
						loginFalse:login
						});
				});
				},(json) => {
				  //失败后
				  //(json.code==53) W2超时
				  const login = (json.code==210);
				  dispatch({
					type: LOAD_ERR,
					message:json.msg+(json.sub_msg?json.sub_msg:""),
					loginFalse:login
					});
			});
		}
	}
}
export function resetLimit(item,value){
	item.is_limit_buy = true;
	item.limit_buy_count = value;
	return {type: RESET_ITEM,item};
}
export function resetLimitItem(ztype,item,value) {
	 return (dispatch) => {
		ajax({
		  api: "math",
		  method: 'GET',
		  data:{price:item.price,value}
		},(json) => {
			item.finaPrice = json.finaPrice;
			item.value = value;
			item.ztype = "discountRate";
			item.promotion_value = value;
			dispatch({
				type: RESET_ITEM,
				item
			});
		},(json) => {
			const login = (json.code==210);
			dispatch({
				type: LOAD_ERR,
				message:json.msg+(json.sub_msg?json.sub_msg:""),
				loginFalse:login
			});
		});
	 }
}
export function resetItem(ztype,item,value) {
	if(ztype=="finalPrice"){
		ztype = "decreaseAmount";
		value = (item.price - value);
	}
	let finaPrice = item.price;
	switch(ztype){
		case "decreaseAmount":
			item.promotion_type=0;
			finaPrice = item.price - value;
		break;
		case "discountRate" :
			item.promotion_type=1;
			finaPrice = item.price * (value/10);
		break;
	}
	item.finaPrice = finaPrice;
	item.value = value;
	item.ztype = ztype;
	item.promotion_value = value;
  return {type: RESET_ITEM,item};
}
export function getList(params) {
    return (dispatch) => {
    //发送请求前
    dispatch({
      type: LOAD_START,
      data: params
    });
	if(window.workbench){
		//千牛环境执行
			let cmd="";
			switch(params.act_type){
				case "item":
					cmd = "taobao.promotionmisc.common.item.detail.list.get"; 
				break;
				case "limit":
					cmd = "taobao.ump.details.get";
					params.act_id=params.activity_id;
				break;
			}
		  window.QN.top.invoke( {
				cmd : 'taobao.promotionmisc.common.item.detail.list.get',
				param : params,
				method:"post",
				success : function(rsp, cmd, param) {
					console.log(rsp,param);
					//先读取列表，再按照ids读取商品信息
					 window.QN.top.invoke( {
						cmd : 'taobao.promotionmisc.common.item.detail.list.get',
						param : {ids:params.ids},
						method:"post",
						success : function(rsp_1, cmd_1, param_1) {
							console.log(rsp_1,param_1);
						},
						error : function(msg, cmd, param) {
							console.log(msg,param);
						}
					} );
				},
				error : function(msg, cmd, param) {
					console.log(msg,param);
				}
			} );
	}else{
		//web环境执行创建折扣
		let cmd="";
		switch(params.act_type){
			case "item":
				cmd = "getItemsInActive";
			break;
			case "limit":
				cmd = "getLimitInActive";
			break;
		}
		ajax({
			  api: cmd,
			  method: 'GET',
			  data:params
			},(json) => {
			  //成功后
				let ids = [];
				switch(params.act_type){
					case "item":
						if(json.total_count>0 && !(json.detail_list.common_item_detail instanceof Array)){
							json.detail_list.common_item_detail = Array(json.detail_list.common_item_detail);
						}
						json.detail_list.common_item_detail.map(det=>{
							ids.push(det.item_id);
						});					
					break;
					case "limit":
						if(json.total_count>0 && !(json.contents.string instanceof Array)){
							json.contents.string = Array(json.contents.string);
						}
						json.contents.string.map(det=>{
							let detObj = JSON.parse(det);
							if(detObj.status=="NORMAL"){
								ids.push(detObj.participateId);
							}
						});	
					break;
				}
				if(ids.length==0){
					let item_json = {items:{item:[]},total_count:0};
					dispatch({
					  type: GET_LIST_DONE,
					  data: item_json
					});
					return;
				}
						ajax({
						  api: 'getItemsByIds',
						  method: 'POST',
						  data:{ids:ids.join(",")}
						},(item_json) => {
							if(item_json.items.item && !(item_json.items.item instanceof Array)){
								item_json.items.item = Array(item_json.items.item);
							}
							
							switch(params.act_type){
								case "item":
									item_json.items.item.map((item,key)=>{
										json.detail_list.common_item_detail.map((det)=>{
											if(det.item_id==item.num_iid){
												item_json.items.item[key].activity_id = det.activity_id;
												item_json.items.item[key].detail_id = det.detail_id;
												item_json.items.item[key].promotion_type = det.promotion_type;
												item_json.items.item[key].promotion_value = det.promotion_value/100;
												item_json.items.item[key].ztype = det.promotion_type==0?"decreaseAmount":"discountRate";
												item_json.items.item[key].value = det.promotion_value/100;
												switch(det.promotion_type){
													case "0" :
														item_json.items.item[key].finaPrice = (item.price - det.promotion_value/100);
													break;
													case "1" :
														item_json.items.item[key].finaPrice = (item.price * (det.promotion_value/1000));
													break;
												}
											};
										});
									});
								break;
								case "limit":
									item_json.items.item.map((item,key)=>{
										json.contents.string.map((det)=>{
											let detObj = JSON.parse(det);
											if(detObj.participateId==item.num_iid){
												let promotion_type=0;
												let promotion_value=0;
												let is_limit_buy=false;
												let limit_buy_count=0;
												detObj.operation.map((opration)=>{
													if(opration.paramId=="discount" && opration.value){
														promotion_type="1";
													}
													if(opration.paramId=="discountRate"){
														promotion_value=opration.value;
													}
													if(opration.paramId=="decrease" && opration.value){
														promotion_type="0";
													}
													if(opration.paramId=="decreaseMoney"){
														promotion_value=opration.value;
													}
													if(opration.paramId=="limitCount"){
														is_limit_buy=true;
														limit_buy_count=opration.value;
													}
												});
												//console.log(promotion_type,promotion_value,detObj);
												item_json.items.item[key].activity_id = params.activity_id;
												item_json.items.item[key].detail_id = detObj.detailId;
												item_json.items.item[key].is_limit_buy = is_limit_buy;
												item_json.items.item[key].limit_buy_count = limit_buy_count;
												item_json.items.item[key].promotion_type = promotion_type;
												item_json.items.item[key].promotion_value = promotion_value/100;
												item_json.items.item[key].ztype = promotion_type=="0"?"decreaseAmount":"discountRate";
												item_json.items.item[key].value = promotion_value/100;
												switch(promotion_type){
													case "0" :
														item_json.items.item[key].finaPrice = (item.price - promotion_value/100);
													break;
													case "1" :
														item_json.items.item[key].finaPrice = (item.price * (promotion_value/1000));
													break;
												}
											}
										});
									});
								break;
							}
							item_json.total_count=ids.length;
							dispatch({
							  type: GET_LIST_DONE,
							  data: item_json
							});
						},(json) =>{
							const login = (json.code==210);
							dispatch({
								type: LOAD_ERR,
								message:json.msg+(json.sub_msg?json.sub_msg:""),
								loginFalse:login
							});
						}
						);
			},(json) => {
			  //失败后
			  //(json.code==53) W2超时
			  const login = (json.code==210);
			  dispatch({
				type: LOAD_ERR,
				message:json.msg+(json.sub_msg?json.sub_msg:""),
				loginFalse:login
				});
		});
	}

  };
}


